import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

const store = new Vuex.Store({
	state: {
		hasLogin: false,
		hasBindCommunity: false,
		hasOwner: false,
		backUrl: '',
		userInfo: {},
		footPrint: [], //商品浏览历史
		currentCity: '', //当前选择城市
		applicationConfig: {}, //应用全局设置
		defaultCommunityId: '1000000000000000',
		tabBar: [{
				"pagePath": "/pages/index/index",
				"iconPath": "/static/tab-home.png",
				"selectedIconPath": "/static/tab-home-current.png",
				"text": "首页",
				"num": 0,
				"isDot": false,
				"verify": true
			},
			{
				"pagePath": "/pages/index/agribusiness",
				"text": "农商",
				"iconPath": "/static/tab-agribusiness-icon.png",
				"selectedIconPath": "/static/tab-agribusiness-icon-active.png",
				"num": 0,
				"isDot": false,
				"verify": true
			},
			{
				"pagePath": "/pages/index/cultural_tourism",
				"text": "文旅",
				"iconPath": "/static/tab-cultural-tourism-icon.png",
				"selectedIconPath": "/static/tab-cultural-tourism-active-icon.png",
				"num": 0,
				"isDot": true,
				"verify": true
			},
			{
				"pagePath": "/pages/index/user",
				"iconPath": "/static/tab-my.png",
				"selectedIconPath": "/static/tab-my-current.png",
				"text": "我的",
				"num": 0,
				"isDot": true,
				"verify": true
			}
		],
		tabBarIndex: 0,
		weChatSetting: {}, // 微信小程序设置的信息
	},
	mutations: {
		addFootPrint(state, provider) {
			var oldFootPrint = state.footPrint;
			var footPrint = [];
			for (var i = 0; i < oldFootPrint.length; i++) {
				if (oldFootPrint[i].productUuid != provider.productUuid) {
					footPrint.push(oldFootPrint[i]);
				}
			}
			footPrint.push(provider);
			//只保留20个商品浏览历史
			if (footPrint.length > 20) {
				footPrint = footPrint.slice(-20);
			}
			state.footPrint = footPrint;
			uni.setStorage({ //缓存应用全局设置
				key: 'footPrint',
				data: footPrint
			})
		},
		clearFootPrint(state) {
			state.footPrint = [];
			uni.setStorage({ //缓存应用全局设置
				key: 'footPrint',
				data: []
			})
		},
		updateApplicationConfig(state, provider) {
			state.applicationConfig = provider;
			uni.setStorage({ //缓存应用全局设置
				key: 'applicationConfig',
				data: provider
			})
		},
		changeCity(state, provider) {
			state.currentCity = provider;
			uni.setStorage({ //缓存当前城市设置
				key: 'currentCity',
				data: provider
			})
		},
		backUrl(state, provider) {
			state.backUrl = provider
		},
		login(state, provider) {
			console.log("缓存登录信息")
			state.hasLogin = true;
			state.userInfo = provider;
			// uni.setStorageSync('loginFlag', true)
			if (provider.personalPhone != null) {
				// 存手机号
				uni.setStorageSync("bindMobileNo", provider.personalPhone)
			}
			if (provider.ownerInfo != null && provider.ownerInfo.ownerId != null) {
				// 绑定真实信息标志
				state.hasOwner = true;
			}
			// 缓存用户信息
			uni.setStorageSync("userInfo", provider)
		},
		logout(state) {
			state.hasLogin = false;
			state.hasOwner = false;
			state.userInfo = {};
			// uni.setStorageSync('loginFlag', false)
			uni.setStorageSync("userToken", '')
			uni.removeStorage({
				key: 'userInfo'
			})
			uni.removeStorage({
				key: 'bindCommunityInfo'
			})
			uni.removeStorage({
				key: 'userToken'
			})
		},
		changeTabBar(state, payload) {
			if (payload) {
				state.tabBarIndex = payload.index
			}
		},
		updateWeCahtSetting(state, provider) {
			state.weChatSetting = provider;
			uni.setStorage({ //缓存应用全局设置
				key: 'weChatSetting',
				data: provider
			})
		},
		setUserInfo(state, provider) {
			state.userInfo = provider;
			uni.setStorage({ //缓存应用全局设置
				key: 'userInfo',
				data: provider
			})
		}
	},
	actions: {

	}
})

export default store
